import { MaterialService } from 'src/app/@core/services/material/material.service';
import { Material } from './../../../../@core/data/material';
import { JobOrderService } from 'src/app/@core/services/jobOrder/job-order.service';
import { Component, Input, ViewChild } from '@angular/core';
import { DataTableComponent, DialogService, TableWidthConfig } from 'ng-devui';
import { StorehouseService } from 'src/app/@core/services/storehouse/storehouse.service';
import { SelectInventoryComponent } from 'src/app/@shared/components/select-inventory/select-inventory.component';
import { SelectPositionComponent } from 'src/app/@shared/components/select-position/select-position.component';
import { SelectStorehouseComponent } from 'src/app/@shared/components/select-storehouse/select-storehouse.component';

@Component({
  selector: 'app-inventory-rk',
  templateUrl: './inventory-rk.component.html',
  styleUrls: ['./inventory-rk.component.scss'],
})
export class InventoryRkComponent {
  @Input() data: any;
  msgs: { severity: any; summary: any; content: any }[];
  isSelected = false;
  displayTableData: any[] = [];
  tableheight = '230px';
  selectData: any;
  pager = {
    total: 20,
    pageIndex: 1,
    pageSize: 20,
  };
  pageSizeOptions = [10, 20, 30, 50, 100];
  dataTableOptions = {
    columns: [
      {
        field: 'name',
        header: '物料编码',
        fieldType: 'text',
      },
      {
        field: 'name',
        header: '物料名称',
        fieldType: 'text',
      },
      {
        field: 'name',
        header: '规格',
        fieldType: 'text',
      },
      {
        field: 'name',
        header: '生产数量',
        fieldType: 'text',
      },
      {
        field: 'name',
        header: '已入库数',
        fieldType: 'text',
      },
      {
        field: 'name',
        header: '入库数量',
        fieldType: 'text',
      },
      {
        field: 'name',
        header: '单位',
        fieldType: 'text',
      },
      {
        field: 'name',
        header: '仓库',
        fieldType: 'text',
      },
      {
        field: 'name',
        header: '仓位',
        fieldType: 'text',
      },
      {
        field: 'name',
        header: '预警时间',
        fieldType: 'text',
      },
      {
        field: 'name',
        header: '备注',
        fieldType: 'text',
      },
      {
        field: 'name',
        header: '安全库存',
        fieldType: 'text',
      },
      {
        field: 'name',
        header: '最低库存',
        fieldType: 'text',
      },
      {
        field: 'name',
        header: '最高库存',
        fieldType: 'text',
      },
    ],
  };
  tableWidthConfig: TableWidthConfig[] = [
    {
      field: '操作',
      width: '100px',
    },
    {
      field: '物料编码',
      width: '150px',
    },
    {
      field: '物料名称',
      width: '150px',
    },
    {
      field: '规格',
      width: '150px',
    },
    {
      field: '生产数量',
      width: '150px',
    },
    {
      field: '已入库数',
      width: '150px',
    },
    {
      field: '入库数量',
      width: '150px',
    },
    {
      field: '单位',
      width: '150px',
    },
    {
      field: '仓库',
      width: '150px',
    },
    {
      field: '仓位',
      width: '150px',
    },
    {
      field: '预警时间',
      width: '220px',
    },
    {
      field: '备注',
      width: '150px',
    },
    {
      field: '安全库存',
      width: '150px',
    },
    {
      field: '最低库存',
      width: '150px',
    },
    {
      field: '最高库存',
      width: '150px',
    },
  ];
  getParts: any;
  tableData = [];
  checkRows: any[] = [];
  isIconRotated = false;
  @ViewChild(DataTableComponent, { static: true })
  datatable: DataTableComponent;
  warehouseData: any;
  constructor(
    private StorehouseService: StorehouseService,
    private dialogService: DialogService,
    private JobOrderService: JobOrderService,
    private MaterialService: MaterialService
  ) {}
  ngOnInit(): void {
    this.tableData[0] = Object.assign({}, this.data);
    this.tableData[0].Quantity = this.data.quantity;
  this.tableData[0].quantity =this.data.quantity - this.calculateEntryQuantitySum(this.data);
  this.tableData[0].warningDate = this.tableData[0].warningDate || null;
    this.MaterialService.getPartById(this.data.partId).subscribe(
      (data: any) => {
        this.getParts = data;
        this.tableData[0].name = this.getParts.partNum;
        this.tableData[0].warehouseId = this.getParts.warehouseID;
        this.tableData[0].positionId = this.getParts.positionID;
        this.tableData[0].safetyInventory = this.getParts.safetyInventory;
        this.tableData[0].minimumStock = this.getParts.minimumStock;
        this.tableData[0].maximumStock = this.getParts.maximumStock;
        this.StorehouseService.getWarehouseById(
          this.getParts.warehouseID
        ).subscribe((data: any) => {
          this.tableData[0].warehouseName = data.warehouseName;
        });
        this.StorehouseService.GetPositionById(
          this.getParts.positionID
        ).subscribe((data: any) => {
          this.tableData[0].positionName = data.positionName;
        });
      }
    );
  }
  pageIndexChange($event: any) {
    this.data.pageNumber = $event;
    this.StorehouseService.GetIssueJobOrderProcessPartsByProcessId(
      this.data
    ).subscribe((data: any) => {
      this.tableData = data.items;
      this.pager.total = data.totalCount;
    });
  } //切换页码
  pageSizeChange($event: any) {
    this.data.pageSize = $event;
    this.StorehouseService.GetIssueJobOrderProcessPartsByProcessId(
      this.data
    ).subscribe((data: any) => {
      this.tableData = data.items;
      this.pager.total = data.totalCount;
    });
  } //改变每页显示数量
  radio(event: any, rowItem: any) {
    if (event.target.checked) {
      this.isSelected = true;
      this.selectData = rowItem;
    }
  }
  delete(index: number, dialogtype?: string) {
    const results = this.dialogService.open({
      id: 'dialog-service',
      showAnimation: true,
      width: '400px',
      title: '删除',
      content: '确认删除该条数据吗？',
      dialogtype: dialogtype,
      onClose: () => {},
      buttons: [
        {
          cssClass: 'primary',
          text: '确定',
          disabled: false,
          handler: ($event: Event) => {
            this.tableData.splice(index, 1);
            results.modalInstance.hide();
          },
        },
        {
          id: 'btn-cancel',
          cssClass: 'common',
          text: '取消',
          handler: ($event: Event) => {
            results.modalInstance.hide();
          },
        },
      ],
    });
  }
  apart(index: number) {
    const item = JSON.parse(JSON.stringify(this.tableData[index])); // 获取索引位置的元素的深拷贝
    this.tableData.splice(index + 1, 0, item); // 在索引+1的位置插入元素
  }
  changeQuantity($event: any, rowIndex: any) {}
  SelectStorehouse(rowItem: any, dialogtype?: string) {
    const results = this.dialogService.open({
      id: 'dialog-service',
      showAnimation: true,
      width: '1000px',
      title: '选择仓库',
      content: SelectStorehouseComponent,
      dialogtype: dialogtype,
      onClose: () => {},
      buttons: [
        {
          cssClass: 'primary',
          text: '确定',
          disabled: false,
          handler: ($event: Event) => {
            if (results.modalContentInstance.selectData) {
              rowItem.warehouseName =
                results.modalContentInstance.selectData.warehouseName;
              rowItem.warehouseNum =
                results.modalContentInstance.selectData.warehouseNum;
              rowItem.warehouseId =
                results.modalContentInstance.selectData.warehouseId;
              this.warehouseData = results.modalContentInstance.selectData;
              rowItem.positionName = null;
              rowItem.positionNum = null;
              rowItem.positionId = null;
            }
            results.modalInstance.hide();
          },
        },
        {
          id: 'btn-cancel',
          cssClass: 'common',
          text: '取消',
          handler: ($event: Event) => {
            results.modalInstance.hide();
          },
        },
      ],
    });
  }
  selectPosition(rowItem: any, dialogtype?: string) {
    if (rowItem.warehouseId == 0) {
      return;
    }
    let data = {
      display: false,
      warehouseData: this.warehouseData,
      warehouseId: rowItem.warehouseId,
      placeholder: '请选择库位',
    };
    const results = this.dialogService.open({
      id: 'dialog-service',
      showAnimation: true,
      width: '1000px',
      data: data,
      title: '选择仓位',
      content: SelectPositionComponent,
      dialogtype: dialogtype,
      onClose: () => {},
      buttons: [
        {
          cssClass: 'primary',
          text: '确定',
          disabled: false,
          handler: ($event: Event) => {
            if (results.modalContentInstance.selectData) {
              rowItem.positionName =
                results.modalContentInstance.selectData.positionName;
              rowItem.positionNum =
                results.modalContentInstance.selectData.positionNum;
              rowItem.positionId =
                results.modalContentInstance.selectData.positionId;
            }
            results.modalInstance.hide();
          },
        },
        {
          id: 'btn-cancel',
          cssClass: 'common',
          text: '取消',
          handler: ($event: Event) => {
            results.modalInstance.hide();
          },
        },
      ],
    });
  }
  calculateEntryQuantitySum(data: any): any {
    return data.productionWarehousingDetails.reduce(
      (sum: number, detail: any) => {
        return sum + detail.entryQuantity;
      },
      0
    );
  }
}
